﻿/********************************************************************
	created:	2011/05/29
	created:	29:5:2011   21:25
	filename: 	D:\My Documents\Visual Studio 2010\Projects\IntranetMessagerClient\Client\RegWindow.cs
	file path:	D:\My Documents\Visual Studio 2010\Projects\IntranetMessagerClient\Client
	file base:	RegWindow
	file ext:	cs
	author:		
	
	purpose:	注册窗口功能实现
*********************************************************************/
using System;
using System.Windows.Forms;
using CommonLibrary;

namespace Client
{
    public partial class RegWindow : Form
    {
        public RegWindow()
        {
            InitializeComponent();
        }

        private void CancelBtnClick(object sender, EventArgs e)
        {
            Close();
        }

        private void PasswordConfirm(object sender, EventArgs e)
        {
            regNoticeLabel.Text = String.Compare(passwordText.Text, confirmPasswordText.Text, false) != 0
                                      ? @"两次输入密码不相同"
                                      : @"密码有效";
        }

        private void RegWindowKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                CancelBtnClick(sender, e);
            }
            else
            {
                CancelBtnClick(sender, e);
            }
        }

        private void GoRegKeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyData)
            {
                case Keys.Enter:
                    RunRegBtnClick(sender, e);
                    break;
                case Keys.Escape:
                    CancelBtnClick(sender, e);
                    break;
            }
        }

        private void RunRegBtnClick(object sender, EventArgs e)
        {
            if (String.IsNullOrWhiteSpace(regNickName.Text) ||
                String.Compare(passwordText.Text, confirmPasswordText.Text, false) != 0)
            {
                regNoticeLabel.Text = @"请检查您输入的信息";
                return;
            }
            var regMessage = new Protocol.RegNewUser {MyName = regNickName.Text, MyPassword = passwordText.Text};
            byte[] messageSend = CommonFunctionClass.SealMessageToEnvelope(regMessage, 0, false, false);
            MainWindow.SendDataDelegateFunc.BeginInvoke(messageSend, null, null);
            regNoticeLabel.Text = @"正在为您注册，请稍后...";
        }

        public void RegisterReponses(string reponsesMessage)
        {
            regNoticeLabel.Text = reponsesMessage;
        }
    }
}